package com.example.alladapter.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.alladapter.Adapter.RecycleViewAdapter;
import com.example.alladapter.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment1 extends Fragment {
    /**
     * 文档浏览
     */

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    private RecyclerView recyclerView;
    private Context mContext;

    public Fragment1() {
    }

    public Fragment1(Context context) {
        mContext = context;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment1 newInstance(String param1, String param2) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        initView(view);
        Operation();
        return view;
    }

    private void Operation() {
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
        RecycleViewAdapter recycleViewAdapter = new RecycleViewAdapter(-1, mContext, new RecycleViewAdapter.OnRecycleClickListen() {
            @Override
            public void Onclick(int pos) {
                Toast.makeText(mContext, "点击了" + pos, Toast.LENGTH_LONG).show();
            }
        });
        recyclerView.setAdapter(recycleViewAdapter);
    }

    private void initView(View view) {
        recyclerView = view.findViewById(R.id.rec);
    }

}